package com.machine.action.search;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.machine.bean.Componentpart;
import com.machine.dao.user.ComponentDao;
import com.machine.util.JSONObject;
import com.opensymphony.xwork2.ActionSupport;

@Component("timeSearchAction")
@Scope("prototype")
public class TimeSearchAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	private String tagnum;
	private ComponentDao componentDao;
	private String timeKind;

	public String getTimeKind() {
		return timeKind;
	}

	public void setTimeKind(String timeKind) {
		this.timeKind = timeKind;
	}

	public ComponentDao getComponentDao() {
		return componentDao;
	}

	@Resource
	public void setComponentDao(ComponentDao componentDao) {
		this.componentDao = componentDao;
	}

	public String getTagnum() {
		return tagnum;
	}

	public void setTagnum(String tagnum) {
		this.tagnum = tagnum;
	}

	@Override
	public String execute() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		String keyword = (String) request.getSession().getAttribute("keywords");
		
		List<Componentpart> comps = componentDao.findByTime(tagnum,keyword,timeKind);
		JSONObject json = new JSONObject();
		
		int count = 0;
		List<String> names = new ArrayList<String>();
		List<String> specifications = new ArrayList<String>();
		List<String> materialqualitys = new ArrayList<String>();
		List<String> providerids = new ArrayList<String>();
		List<String> outtimes = new ArrayList<String>();
		List<String> comnums = new ArrayList<String>();
		List<Float> prices = new ArrayList<Float>();
		List<Integer> ids = new ArrayList<Integer>();
		List<String> sellNum = new ArrayList<String>();
		List<String> stockNum = new ArrayList<String>();
		
		
		List<String> counts = new ArrayList<String>();
		for(Componentpart c : comps){	
			names.add(c.getName());
			specifications.add(c.getSpecification());
			materialqualitys.add(c.getMaterialquality());
			providerids.add(String.valueOf(c.getProviderid()));
			outtimes.add(c.getOuttime());
			comnums.add(c.getComponentnumber());
			count = comps.size();
			counts.add("1");
			prices.add(c.getPrice());
			ids.add(c.getId());
			sellNum.add(String.valueOf(c.getSellnum()));
			stockNum.add(String.valueOf(c.getStockamount()));
		}
		
		json.put("name", names);
		json.put("specification", specifications);
		json.put("materialquality", materialqualitys);
		json.put("providerid", providerids);
		json.put("outtime", outtimes);
		json.put("componentnum", comnums);
		json.put("size", count);
		json.put("price", prices);
		json.put("id", ids);
		json.put("sell", sellNum);
		json.put("stock", stockNum);
		//System.out.println(c.getName());
//		System.out.println(json.toString());
		
		request.setAttribute("json", json.toString());
		request.setAttribute("count", counts);
		request.setAttribute("tag", tagnum);
		return SUCCESS;
	}
}
